package ee.videorentalstore.protocols;

import java.util.List;
import ee.videorentalstore.protocols.commands.Command;

public abstract class Protocol 
{
	private final String COMMAND_DESCRIPTION_AND_ITS_CONTENTS_SPLITTING_CONSTANT = "----";
	
	
	public abstract void runUserProtocol();
	
	
	protected void parseCommandFromInstructionsAndExecute(
			String instructionsInsertedByUserToProcess,
			List<Command> listOfCommandsThatCanBeExecutedInAProtocol)
	{	
		for (Command command : listOfCommandsThatCanBeExecutedInAProtocol)
		{
			if (getCommandDescriptionToExecuteOutOfInstructions(instructionsInsertedByUserToProcess).
					equalsIgnoreCase(command.getCommandDescription()))
			{
				command.executeCommand(instructionsInsertedByUserToProcess); 
				return; 
			}
		}
		
		System.out.println("Command can not be found. Type HELP if you need an advice!");
	}
	
	
	private String getCommandDescriptionToExecuteOutOfInstructions(
			String instructionsInsertedByUserToProcess)
	{
		int beginIndexOfACommandDescriptionToExecute = 0;
		int endIndexOfACommandDescriptionToExecute = instructionsInsertedByUserToProcess.indexOf(
				COMMAND_DESCRIPTION_AND_ITS_CONTENTS_SPLITTING_CONSTANT);
		
		String commandDescriptionToExecute = instructionsInsertedByUserToProcess.substring(
				beginIndexOfACommandDescriptionToExecute, endIndexOfACommandDescriptionToExecute);
		return commandDescriptionToExecute.trim();
	}
}
